//
//  LZStatusOriginView.swift
//  Weibo
//
//  Created by lzing on 16/3/25.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SnapKit

class LZStatusOriginView: UIView {
    
    /// 底部约束
    var originalViewBottomConstraint : Constraint?
    
    //MARK: - property
    ///  模型
    var status : LZStatus? {
        didSet {
            // 设置头像
            if let image = status?.user?.profile_image_url {
                let url = NSURL(string: image)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            
            // 设置名称
            nameLabel.text = status?.user?.screen_name
            
            // 设置时间
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDateDescripiton()
            
            // 设置来源
            sourceLabel.text = status?.source
            
            // 设置会员等级
            memberView.image = status?.user?.mbrankView
            
            // 设置认证图标
            verifiedView.image = status?.user?.verified_typeImage
            
            // 设置微博内容
            contentLabel.attributedText = status?.attributeText
            
            // 卸载约束
            originalViewBottomConstraint?.uninstall()
            // 设置图片
            if let pictureURLs = status?.pictureURLs {
                // 图片数组有值
                // 显示图片
                pictureView.hidden = false
                // 设置属性
                pictureView.pictureURLs = pictureURLs
                // 重新设置约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LZStatusCellMargin).constraint
                })
                
            } else {
                // 隐藏图片
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(LZStatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        ///  添加子控件
        addSubviews()
        ///  添加约束
        addConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - prepareUI
    ///  添加子控件
    private func addSubviews() {
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
    }
    
    ///  添加约束
    private func addConstraints() {
        // 头像约束
        iconView.snp_makeConstraints { (make) -> Void in
            // 左边与上边等于父控件的左边与上边
            make.left.equalTo(self).offset(LZStatusCellMargin)
            make.top.equalTo(self).offset(LZStatusCellMargin)
            // 大小为35X35
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        // 名字约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            // 左边等于头像的右边偏移一个间距
            make.left.equalTo(iconView.snp_right).offset(LZStatusCellMargin)
            // 顶部等于头像
            make.top.equalTo(iconView)
        }
        
        // 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            // 左边等于名字Label的右边偏移一个间距
            make.left.equalTo(nameLabel.snp_right).offset(LZStatusCellMargin)
            // 中心Y等于名字label
            make.centerY.equalTo(nameLabel)
            // 大小等于14X14
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            // 左边等于头像的右边
            make.left.equalTo(iconView.snp_right).offset(LZStatusCellMargin)
            // 底部等于头像底部
            make.bottom.equalTo(iconView)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            // 中心Y等于时间Label
            make.centerY.equalTo(timeLabel)
            // 左边等于时间Label的右边
            make.left.equalTo(timeLabel.snp_right).offset(LZStatusCellMargin)
        }
        
        // 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            // 中心X等于头像的右边
            make.centerX.equalTo(iconView.snp_right)
            // 中心Y等于头像的底部
            make.centerY.equalTo(iconView.snp_bottom)
            // 大小等于17X17
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        // 微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            // 顶部等于头像的底部偏移一个间距
            make.top.equalTo(iconView.snp_bottom).offset(LZStatusCellMargin)
            // 左边等于父控件偏移一个间距
            make.left.equalTo(self).offset(LZStatusCellMargin)
            // 右边等于父控件偏移一个间距
            make.right.equalTo(self).offset(-LZStatusCellMargin)
        }

        // 配图
        pictureView.snp_makeConstraints { (make) -> Void in
            // 顶部等于内容的底部偏移一个间距
            make.top.equalTo(contentLabel.snp_bottom).offset(LZStatusCellMargin)
            // 左边等于内容的左边
            make.left.equalTo(contentLabel)
            // 大小为200X200
            make.size.equalTo(CGSize(width: 200, height: 200))
        }

        // 原创cell的底部参照微博内容
        self.snp_makeConstraints { (make) -> Void in
            // 底部等于内容的底部偏移一个间距,并且记录约束
            self.originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(LZStatusCellMargin).constraint
        }
    }
    
    //MARK: - lazy
    /// 头像
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    /// 名字
    private lazy var nameLabel : UILabel = {
        
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: LZStatusCellContentSize)
        
        return label
    }()
    
    /// 时间
    private lazy var timeLabel : UILabel = {
        
        let label = UILabel(color: UIColor.orangeColor(), fontSize: LZStatusCellDefaultFont)
        
        return label
    }()
    
    /// 来源
    private lazy var sourceLabel : UILabel = {
        let label = UILabel(color: UIColor.lightGrayColor(), fontSize: LZStatusCellDefaultFont)
        
        return label
    }()
    
    /// 会员等级
    private lazy var memberView : UIImageView = UIImageView()
    
    /// 认证图标
    private lazy var verifiedView : UIImageView = UIImageView()
    
    /// 内容文本
    private lazy var contentLabel : UILabel = {
        
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: LZStatusCellContentSize)

        label.numberOfLines = 0
        // 设置label宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * LZStatusCellMargin
        
        return label
    }()
    
    /// 配图
    private lazy var pictureView : LZStatusPictureView = LZStatusPictureView()
}
